package process
import (
	"fmt"
	"net"
	"manyuser/common/message"
	"manyuser/server/utils"
	"encoding/json"
	"manyuser/server/model"
)

type UserProcess struct{
	Conn net.Conn
	UserId int 
}
//登录下消息类型处理
func (this *UserProcess)ServerProcessLogin(mes *message.Message)(err error){
	var loginMes message.LoginMes
	err=json.Unmarshal([]byte(mes.Data),&loginMes)
	if err!=nil{
		fmt.Println("json.Unmarshal([]byte(message.data) is wrong",err)
		return
	}

	//结果消息类型
	var resMes message.Message
	resMes.Type=message.LoginResMesType
	var loginResMes message.LoginResMes
	
	user,err:=model.MyUserDao.Login(loginMes.UserId,loginMes.UserPwd)
	if err!=nil{
		if err==model.ERROR_USER_NOTEXISTS{
			loginResMes.Code=500
			loginResMes.Error=err.Error()
		}else if err==model.ERROR_USER_PWD{
			loginResMes.Code=403
			loginResMes.Error=err.Error()
		}else{
			loginResMes.Code=505
			loginResMes.Error="server is wrong"
		}

	}else{
		loginResMes.Code=200
		fmt.Println(user,"login success")
	
		//将当前用户加入进去
		this.UserId=loginMes.UserId
		userMgr.AddOnlineUser(this)
		//通知其他上线用户
		this.NotifyOthersOnlinerUser(loginMes.UserId)

		for id,_:=range userMgr.onlineUsers{
			loginResMes.UsersId=append(loginResMes.UsersId,id)
		}

	}
	
	data,err:=json.Marshal(loginResMes)
	if err!=nil{
		fmt.Println("json.Marshal(loginResMes) is wrong",err)
		return
	}
	resMes.Data=string(data)
	data,err=json.Marshal(resMes)
	if err!=nil{
		fmt.Println("json.Marshal(resMes) is wrong",err)
		return
	}
	//发送消息
	
	err=(&utils.Transfer{Conn:this.Conn,}).WritePkg(data)
	return
}

//注册消息类型型处理
func (this *UserProcess)ServerProcessRegister(mes *message.Message)(err error){
	var registerMes message.RegisterMes
	err=json.Unmarshal([]byte(mes.Data),&registerMes)
	if err!=nil{
		fmt.Println("json.Unmarshal([]byte(message.data) is wrong",err)
		return
	}

	//结果消息类型
	var resMes message.Message
	resMes.Type=message.RegisterResMesType
	var registerResMes message.RegisterResMes
	
	err=model.MyUserDao.Register(&registerMes.User)
	if err!=nil{
		if err==model.ERROR_USER_EXISTS{
			registerResMes.Code=505
			registerResMes.Error=err.Error()
		}else{
			registerResMes.Code=506
			registerResMes.Error="server is unkonwn wrong at registering"
		}

	}else{
		registerResMes.Code=200
		fmt.Println(registerMes.User,"login success")
	}
	
	data,err:=json.Marshal(registerResMes)
	if err!=nil{
		fmt.Println("json.Marshal(loginResMes) is wrong",err)
		return
	}
	resMes.Data=string(data)
	data,err=json.Marshal(resMes)
	if err!=nil{
		fmt.Println("json.Marshal(resMes) is wrong",err)
		return
	}
	//发送消息
	
	err=(&utils.Transfer{Conn:this.Conn,}).WritePkg(data)
	return
}

//用户上线后通知其他用户方法
func (this *UserProcess)NotifyOthersOnlinerUser(userId int ){
	for id,up:=range userMgr.onlineUsers{
		if id==userId{
			continue
		}else{
			up.NotifyMeOnline(userId)
		}
	}
}

func (this *UserProcess)NotifyMeOnline(userId int){
	var mes message.Message
	mes.Type=message.NotifyUserStatusMesType

	var notifyUserStatusMes message.NotifyUserStatusMes
	notifyUserStatusMes.UserId=userId
	notifyUserStatusMes.Status=message.UserOnline

	data,err:=json.Marshal(notifyUserStatusMes)
	if err!=nil{
		fmt.Println("json.Marshal(notifyUserStatusMes) wrong,",err)
		return
	}
	mes.Data=string(data)

	data,err=json.Marshal(mes)
	if err!=nil{
		fmt.Println("json.Marshal(mes) wrong,",err)
		return
	}
	err=(&utils.Transfer{Conn:this.Conn,}).WritePkg(data)
	if err!=nil{
		fmt.Println("(&utils.Transfer{Conn:this.Conn,}).WritePkg(data) wrong,",err)
		return
	}
}